<template>
  <login-header>联合登录</login-header>
  <section class="container" v-if="isBind">
    <div class="unbind">
      <div class="loading"></div>
    </div>
  </section>
  <section class="container" v-else>
    <nav class="tab">
      <a
        @click="hasAccount = true"
        :class="{ active: hasAccount }"
        href="javascript:;"
      >
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a
        @click="hasAccount = false"
        :class="{ active: !hasAccount }"
        href="javascript:;"
      >
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <CallbackBind />
    </div>
    <div class="tab-content" v-else>
      <CallbackPatch />
    </div>
  </section>
  <login-footer />
</template>
<script>
import LoginHeader from "./components/LoginHeader";
import LoginFooter from "./components/LoginFooter";
import CallbackBind from "@/views/login/components/callbackBind";
import CallbackPatch from "@/views/login/components/callbackPatch";
import { userQQLogin } from "@/api/user";
import { ref } from "vue";
import QC from "qc";
export default {
  name: "LoginCallback",
  components: { LoginHeader, LoginFooter, CallbackBind, CallbackPatch },
  setup() {
    const hasAccount = ref(false);
    // 首先默认认为已经注册并且绑定
    const isBind = ref(true);
    // 通过QQ的API获取openId就是后台需要的unionId然后去登录
    // 成功:登录成功 失败:该QQ未与平台绑定(有账号未绑定,没有账号未绑定)
    if (QC.Login.check()) {
      // openId:第三方QQ的唯一标识
      QC.Login.getMe((openId) => {
        console.log(openId);
        // 请求后台,做QQ登录
        userQQLogin(openId)
          .then((data) => {
            console.log(data);
            // 登陆成功,data.result就是用户信息
          })
          .catch((err) => {
            console.log(err);
            isBind.value = false;
            // 登录失败:该QQ未与平台绑定(有账号未绑定,没有账号未绑定)
          });
      });
    }
    return {
      hasAccount,
      isBind,
    };
  },
};
</script>
<style lang="less" scoped>
.container {
  padding: 25px 0;
  position: relative;
  height: 730px;
  .unbind {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 25px 0;
    z-index: 99;
    .loading {
      height: 100%;
      background: #fff url(../../assets/images/load.gif) no-repeat center /
        100px 100px;
    }
  }
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>